package com.handydata.antifraud.rule.funcs;

import com.handydata.antifraud.to.RiskInputTO;
import com.handydata.antifraud.to.RiskMiddleTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Optional;
@Slf4j
@Component
public class CalcSensorAcce extends CalculateAdapter {
    @Override
    public void doCalc(RiskInputTO riskInputTO, RiskMiddleTO riskMiddleTO) {
        double x = 0, y = 0, z = 0, v = 0;

        log.info("start cacl CalcSensorAcce sensorAcce{}",riskInputTO.getSensorAcce());
        String sensorAcce = riskInputTO.getSensorAcce();
        if(!StringUtils.isEmpty(sensorAcce)) {
            String[] coor = sensorAcce.split(",");

            x = Integer.valueOf(Optional.ofNullable(coor[0].split("=")[1]).orElse("0"));
            y = Integer.valueOf(Optional.ofNullable(coor[1].split("=")[1]).orElse("0"));
            z = Integer.valueOf(Optional.ofNullable(coor[2].split("=")[1]).orElse("0"));

            v = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2));
        }

        riskMiddleTO.setSensorAcceX(x);
        riskMiddleTO.setSensorAcceY(y);
        riskMiddleTO.setSensorAcceZ(z);

        log.info("finish cacl CalcSensorAcce x{} y{} z{}",x,y,z);

    }
}
